<!DOCTYPE html>
<html lang="zh">
<head>
    #@header()
    #set(isAdd = adm == null ? true : false, isEdit = !isAdd)
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-role-add">
        <input hidden name="id" id="userId" value="#(adm.id??)">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required is-required">昵称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="nickname" id="nickname" value="#(adm.nickname??)" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">账户：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="username" id="username" value="#(adm.username??)" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">密码：</label>
            <div class="col-sm-8">
                <input class="form-control" type="password" name="password" id="password" value="#(adm.password??)" required>
            </div>
        </div>

        #if(adm != null && adm.id !=1)
        <div class="form-group">
            <label class="col-sm-3 control-label">解锁/上锁：</label>
            <div class="col-sm-8">
                <label class="toggle-switch switch-solid">
                    <input type="checkbox" id="status" #if(adm == null ) checked #elseif( adm?? && adm.status == 1) checked  #end >
                    <span></span>
                </label>
            </div>
        </div>
        #end

        #if(isAdd)
        <div class="form-group">
            <label class="col-sm-3 control-label">解锁/上锁：</label>
            <div class="col-sm-8">
                <label class="toggle-switch switch-solid">
                    <input type="checkbox" id="status" #if(adm == null ) checked #elseif( adm?? && adm.status == 1) checked  #end >
                    <span></span>
                </label>
            </div>
        </div>

        #end

        <label class="col-sm-3 control-label">性别：</label>
        <div class="col-sm-9">
            #if(isAdd)
            <label class="radio-inline">
                <input type="radio" checked value="1" name="sex">男</label>
            <label class="radio-inline">
                <input type="radio" value="1" name="sex">女</label>
            #else
            <label class="radio-inline">
                <input type="radio" #if(adm != null && adm.sex == 1) checked #end value="1" name="sex">男</label>
            <label class="radio-inline">
                <input type="radio" #if(adm != null && adm.sex == 2) checked #end value="2" name="sex">女</label>
            #end
        </div>


        #if(account.id == 1 )
        <div class="form-group">
            <label class="col-sm-3 control-label">菜单权限：</label>
            <div class="col-sm-8">
                <label class="check-box">
                    <input type="checkbox" value="1">展开/折叠</label>
                <label class="check-box">
                    <input type="checkbox" value="2">全选/全不选</label>
                <label class="check-box">
                    <input type="checkbox" value="3" checked disabled>父子联动</label>
                <div id="menuTrees" class="ztree ztree-border"></div>

                #if(account.id == 1)
                <span style="color: red">注：超级管理员 默认享有菜单最高权限资格</span>
                #end
            </div>

        </div>
        #end



    </form>
</div>
#@footer()
<script type="text/javascript">

    $(function() {

        var url = "#(prj)/admin/menu/roleMenuTreeData/#(isAdd ? 'add' : 'edit')-#(adm != null ? adm.id : 0)";
        var options = {
            id: "menuTrees",
            url: url,
            check: { enable: true },
            expandLevel: 0
        };
        $.tree.init(options);
    });


    $("#form-role-add").validate({
        rules: {
            nickname: {
                required: true,
                maxlength: 20,
            },
            username:{
                required:true,
                remote: {
                    url: "#(prj)/admin/account/usernameUnique",
                    type: "get",
                    dataType: "json",
                    data: {
                        "id": function() {
                            return $("#userId").val();
                        },
                        "username": function() {
                            return $("input[name='username']").val();
                        }
                    },
                    dataFilter: function (data, type) {
                        var res = JSON.parse(data);
                        if(res.state == 'ok'){
                            return true
                        }else{
                            return false
                        }
                    }
                }
            },
            password: {
                required: true,
                minlength: 5
            },
            mobile: {
                required: true,
                isPhone: true,
            },
            confirm_password: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
            email: {
                required: true,
                email: true
            },
            onkeyup: true,

        },
        messages: {
            "username": {
                remote: "账户已存在"
            },
        },
        focusCleanup: true
    });

    $('input').on('ifChanged', function (obj) {
        var type = $(this).val();
        var checked = obj.currentTarget.checked;
        if (type == 1) {
            if (checked) {
                $._tree.expandAll(true);
            } else {
                $._tree.expandAll(false);
            }
        } else if (type == "2") {
            if (checked) {
                $._tree.checkAllNodes(true);
            } else {
                $._tree.checkAllNodes(false);
            }
        } else if (type == "3") {
            if (checked) {
                $._tree.setting.check.chkboxType = {"Y": "ps", "N": "ps"};
            } else {
                $._tree.setting.check.chkboxType = {"Y": "", "N": ""};
            }
        }
    })

    var uurl = "";
    #if(isAdd)
        uurl = "#(prj)/admin/account/save";
    #else
    uurl = "#(prj)/admin/account/update";
    #end

    function submitHandler() {

        if ($.validate.form()) {
            #(isAdd ? 'add()' : 'edit()')
        }
    }

    function #(isAdd ? 'add()' : 'edit()') {
        var nickname = $("input[name='nickname']").val();
        var username = $("input[name='username']").val();
        var password = $("input[name='password']").val();
        var idd = $("input[name='id']").val();
        var sex = $("input[name='sex']:checked").val();

        var status = $("input[id='status']").is(':checked') ? 1 : 0;

        var formData = new FormData();
        formData.append("adm.nickname",nickname)
        formData.append("adm.username",username)
        formData.append("adm.password",password)
        formData.append("adm.status",status)
        formData.append("adm.sex",sex)
        formData.append("adm.id",idd)
        formData.append("permission",$.tree.getCheckedNodes())

        $.ajax({
            url:uurl,
            type: "post",
            data: formData,
            contentType: false,
            processData: false,
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $.operate.successCallbackMsg(data.msg);
            }
        });
    }

</script>
</body>
</html>
